module.exports   B
last analyzed

Complexity

Conditions 2
Paths 9

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 48
c 1
b 1
f 0
nc 9
dl 0
loc 73
rs 8.7018
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
F 0 64 17

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
const ApiError = require('../util/api_error')
2
const Constant = require('../libraries/constant')
0 ignored issues
show
Unused Code introduced by
The constant Constant seems to be never used. Consider removing it.
Loading history...
3
4
module.exports = function (ctx, rules, message = {}) {
5
    let msgKey
6
    let input = ctx.input
7
8
    for (let field in rules) {
9
        rules[field].split('|').forEach(rule => {
10
            // 普通的message key名称
11
            msgKey = field + '.' + rule
12
13
            if (rule === 'required') {
14
                if (!!input[field] === false) {
15
                    let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能为空'
16
                    throw new ApiError('validate.error', msg)
17
                }
18
                return true
19
            }
20
21
            // 非必填项,有值再进行判断
22
            if (!!input[field] === false) return true
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
23
24
            // 特殊条件
25
            if (rule.indexOf(':') > 0) {
26
                let checkRule = rule.split(':')
27
                msgKey = field + '.' + checkRule[0]
28
                switch (checkRule[0]) {
29
                    case 'in':
30
                        let ruleData = checkRule[1].split(',')
31
                        // 数组
32
                        if (ruleData instanceof Array === true) {
33
                            if (ruleData.indexOf(input[field]) === -1) {
34
                                let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 非法'
35
                                throw new ApiError('validate.error', msg)
36
                            }
37
                        } else {
38
                            let msg = '规则设置错误'
39
                            throw new ApiError('validate.error', msg)
40
                        }
41
                        break
42
                    case 'min':
43
                        // todo
44
                        break
45
                    case 'max':
46
                        // todo
47
                        break
48
                    default:
49
                        console.log('error “:” rule: ' + rule)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
50
                        let msg = '参数验证详细规则错误'
51
                        throw new ApiError('validate.error', msg)
52
                        break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
53
                }
54
                return true
55
            }
56
57
            // 简单条件
58
            switch (rule) {
59
                case 'numeric':
60
                    if (isNaN(input[field]) === true) {
61
                        let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 必须为数字'
62
                        throw new ApiError('validate.error', msg)
63
                    }
64
                    break
65
                default:
66
                    console.log('error rule: ' + rule)
67
                    let msg = '参数验证规则错误'
68
                    throw new ApiError('validate.error', msg)
69
                    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
70
            }
71
            return true
72
        })
73
74
    }
75
76
}